//
//  WFSelectedTableView.m
//  FAKER
//
//  Created by Jianwei on 15/11/4.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "WFSelectedTableView.h"
#import "AllHeader.h"
#import "WFSelectedTableViewCell.h"
#import "FitHeader.h"
#define BOTTOM_SECTION_URLSTRING @"http://iphone.myzaker.com/zaker/find.php?_appid=iphone&_version=6.4"
@implementation WFSelectedTableView
//- (void)pushViewWithWFSelectViewModel:(WFSelectedViewModel *)model
//{
//        [self pushViewWithWFSelectViewModel:model];
//    
//}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _selectedDataSourceArray = [NSMutableArray array];
        [self getData];
        _selectedTableView = [[BaseClassTableView alloc]initWithFrame:CGRectMake(X, Y, SCREEN_WIDTH, HEIGHT) style:UITableViewStylePlain];
        _selectedTableView.dataSource = self;
        _selectedTableView.delegate = self;
//        WFSelectedView *selectedView = [[WFSelectedView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150 * FITWIDTH)];
//        selectedView.delegate = self;
//        _selectedTableView.tableHeaderView = selectedView;
        
        _selectedTableView.tableFooterView = [[BaseClassView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self addSubview:_selectedTableView];
    }
    return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identier = @"identifier";
    WFSelectedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identier];
    if (!cell) {
        cell = [[WFSelectedTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identier];
        cell.model = _selectedDataSourceArray[indexPath.row];
        cell.selectionStyle = 0;
    }
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _selectedDataSourceArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * FITWIDTH;
}

- (void)getData
{
    [InternetDataRequest getDataByString:BOTTOM_SECTION_URLSTRING BodyDic:nil WithDataBlock:^(id data) {
        [self.delegate yhRemoveLoadingView];
        NSArray *array = [[data objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dic  in array) {
            WFSelectedModel *model = [[WFSelectedModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_selectedDataSourceArray addObject:model];
        }
        [_selectedTableView reloadData];
    }];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 18)];
    view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    if (NSUSERDEFAULTS_NIGHT) {
        view.backgroundColor = [UIColor blackColor];
    }
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, view.WIDTH - 15, view.HEIGHT)];
    titleLabel.font = [UIFont systemFontOfSize:10];
    titleLabel.textColor = [UIColor colorWithRed:0.84 green:0.84 blue:0.84 alpha:1];
    titleLabel.text = @"精选";
    [view addSubview:titleLabel];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 18;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate pushViewWithUrlString:[[_selectedDataSourceArray[indexPath.row] block_info] objectForKey:@"api_url"]];

}
@end
